package com.netease.nim.uikit.business.session.viewholder;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.netease.nim.uikit.R;
import com.netease.nim.uikit.common.ui.recyclerview.adapter.BaseMultiItemFetchLoadAdapter;
import com.netease.nim.uikit.common.util.string.StringUtil;
import com.netease.nim.uikit.impl.NimUIKitImpl;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.vondear.rxtool.RxActivityTool;

import java.util.Map;

/**
 * Created by zhoujianghua on 2015/8/6.
 */
public class MsgViewHolderUnknown extends MsgViewHolderBase {
    private LinearLayout linear;
    private ImageView image;
    private TextView title;
    private TextView content;
    private ImageView vedioImg;
    private ImageView headPic;
    private TextView name;
    private RelativeLayout imageRelation;
    private View line;


    public MsgViewHolderUnknown(BaseMultiItemFetchLoadAdapter adapter) {
        super(adapter);
    }

    @Override
    public int getContentResId() {
        return R.layout.item_message_system;
    }

    @Override
    public void inflateContentView() {
        line = findViewById(R.id.line);
        linear = findViewById(R.id.linear);
        image = findViewById(R.id.image);
        title = findViewById(R.id.title);
        content = findViewById(R.id.content);
        vedioImg = findViewById(R.id.videoImg);
        name = findViewById(R.id.memberName);
        headPic = findViewById(R.id.headPic);
        imageRelation = findViewById(R.id.imageRelation);
    }

    private static final String TAG = "MsgViewHolderShare";
    @Override
    public void bindContentView() {
        Log.i("JSON:",message.getAttachStr());
        Map msgMap = new Gson().fromJson(message.getAttachStr(),Map.class);
        Map<String,Object> map = new Gson().fromJson(msgMap.get("ext").toString(),new TypeToken<Map<String,Object>>(){}.getType());
        String titleInfo = map.get("title").toString();
        String contentInfo = map.get("content").toString();
        String typeInfo = map.get("type").toString();
        String idInfo = map.get("msgId").toString();
        title.setText(titleInfo);
        content.setText(contentInfo);

        if(map.get("memberName")!=null){
            name.setText(map.get("memberName").toString());
        }
        if(map.get("headPic")!=null){
            Glide.with(context)
                    .asDrawable()
                    .load(map.get("headPic").toString())
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(headPic);
        }
        if(map.get("imagePath")!=null){
            Glide.with(context).load(map.get("imagePath").toString()).error(com.netease.nim.uikit.R.drawable.nim_default_img_failed).into(image);
            imageRelation.setVisibility(View.VISIBLE);
        }else{
            imageRelation.setVisibility(View.GONE);
        }
        vedioImg.setVisibility(View.GONE);
        if("2".equals(typeInfo)){
            vedioImg.setVisibility(View.VISIBLE);
        }else if(typeInfo.contains("3")){
            name.setVisibility(View.GONE);
            headPic.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
        }else if(typeInfo.contains("4")){
            name.setVisibility(View.GONE);
            headPic.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
        }
        linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NimUIKitImpl.getSessionListener().openSystemActivity(context,typeInfo,idInfo);
            }
        });
    }

    @Override
    protected boolean isShowBubble() {
        return false;
    }
}
